home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 5 / BBS in a Box -Volume V (BBS in a Box) (April 1992).iso / Files / Prog / M / MPWGCC (Sources).cpt / Sources / stor-layout.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-03-15  |  31.2 KB  |  1,073 lines  |  [TEXT/MPS ]

  1. /* C-compiler utilities for types and variables storage layout
  2.    Copyright (C) 1987, 1988 Free Software Foundation, Inc.
  3.    Copyright (C) 1989, 1990 Apple Computer, Inc.
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 1, or (at your option)
  10. any later version.
  11.  
  12. GNU CC is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU CC; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21.  
  22. #include "config.h"
  23. #include <stdio.h>
  24.  
  25. #include "tree.h"
  26. #include "rtl.h"   /* For GET_MODE_SIZE */
  27.  
  28. #define MAX(x,y) ((x) > (y) ? (x) : (y))
  29. #define MIN(x,y) ((x) < (y) ? (x) : (y))
  30. #define CEIL(x,y) (((x) + (y) - 1) / (y))
  31.  
  32. /* Data type for the expressions representing sizes of data types.
  33.    It is the first integer type laid out.
  34.    In C, this is int.  */
  35.  
  36. tree sizetype;
  37.  
  38. /* An integer constant with value 0 whose type is sizetype.  */
  39.  
  40. tree size_zero_node;
  41.  
  42. /* An integer constant with value 1 whose type is sizetype.  */
  43.  
  44. tree size_one_node;
  45.  
  46. #define GET_MODE_ALIGNMENT(MODE)   \
  47.   MIN (BIGGEST_ALIGNMENT,        \
  48.        MAX (1, (GET_MODE_UNIT_SIZE (MODE) * BITS_PER_UNIT)))
  49.  
  50. /* Chain of all permanent types we have allocated since last
  51.    call to get_permanent_types.  */
  52.  
  53. tree permanent_type_chain;
  54.  
  55. /* Chain of all temporary types we have allocated in this function.  */
  56.  
  57. tree temporary_type_chain;
  58.  
  59. /* When the chains is not null, these point at the last
  60.    types on the two chains.  These help us tell whether a type
  61.    is already on a chain.  */
  62. tree permanent_type_end;
  63. tree temporary_type_end;
  64.  
  65. /* Put the newly-made type T
  66.    on either permanent_type_chain or temporary_type_chain.
  67.    Types that are const or volatile variants of other types
  68.    are not put on any chain, since in the gdb symbol segment
  69.    we do not make those distinctions.
  70.  
  71.    If T is already on the chain, we do nothing.  */
  72.  
  73. void
  74. chain_type (t)
  75.      tree t;
  76. {
  77.   if (TYPE_MAIN_VARIANT (t) != t)
  78.     return;
  79.   if (TREE_CHAIN (t) != 0)
  80.     return;
  81.   if (TREE_PERMANENT (t))
  82.     {
  83.       /* If T is on the chain at the end, don't chain it to itself!  */
  84.       if (t == permanent_type_end)
  85.     return;
  86.       /* Add T to the end of the chain.  */
  87.       if (permanent_type_chain == 0)
  88.     permanent_type_chain = t;
  89.       else
  90.     TREE_CHAIN (permanent_type_end) = t;
  91.       permanent_type_end = t;
  92.     }
  93.   else
  94.     {
  95.       if (t == temporary_type_end)
  96.     return;
  97.       if (temporary_type_chain == 0)
  98.     temporary_type_chain = t;
  99.       else
  100.     TREE_CHAIN (temporary_type_end) = t;
  101.       temporary_type_end = t;
  102.     }
  103. }
  104.  
  105. /* Get a chain of all permanent types made since this function
  106.    was last called.  */
  107.  
  108. tree
  109. get_permanent_types ()
  110. {
  111.   register tree tem = permanent_type_chain;
  112.   permanent_type_chain = 0;
  113.   permanent_type_end = 0;
  114.   return tem;
  115. }
  116.  
  117. /* Get a chain of all temporary types made since this function
  118.    was last called.  */
  119.  
  120. tree
  121. get_temporary_types ()
  122. {
  123.   register tree tem = temporary_type_chain;
  124.   temporary_type_chain = 0;
  125.   temporary_type_end = 0;
  126.   return tem;
  127. }
  128.  
  129. /* SAVE_EXPRs for sizes of types and decls, waiting to be expanded.  */
  130.  
  131. static tree pending_sizes;
  132.  
  133. /* Nonzero means cannot safely call expand_expr now,
  134.    so put variable sizes onto `pending_sizes' instead.  */
  135.  
  136. int immediate_size_expand;
  137.  
  138. tree
  139. get_pending_sizes ()
  140. {
  141.   tree chain = pending_sizes;
  142.   pending_sizes = 0;
  143.   return chain;
  144. }
  145.  
  146. /* Given a size SIZE that isn't constant, return a SAVE_EXPR
  147.    to serve as the actual size-expression for a type or decl.  */
  148.  
  149. static tree
  150. variable_size (size)
  151.      tree size;
  152. {
  153.   size = save_expr (size);
  154.  
  155.   if (global_bindings_p ())
  156.     {
  157.       error ("variable-size type declared outside of any function");
  158.       return build_int (1);
  159.     }
  160.  
  161.   if (immediate_size_expand)
  162.     expand_expr (size, 0, VOIDmode, 0);
  163.   else
  164.     pending_sizes = tree_cons (0, size, pending_sizes);
  165.  
  166.   return size;
  167. }
  168.  
  169. /* Return the machine mode to use for an aggregate of SIZE bits.
  170.  
  171.    Note!!!  We only use a non-BLKmode mode if the size matches exactly.
  172.    There used to be the idea of using DImode for anything whose
  173.    size was less than DImode but more than SImode.  This does not work
  174.    because DImode moves cannot be used to store such objects in memory.  */
  175.  
  176. #ifndef MAX_FIXED_MODE_SIZE
  177. #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
  178. #endif
  179.  
  180. static
  181. enum machine_mode
  182. agg_mode (size)
  183.      unsigned int size;
  184. {
  185.   register int units = size / BITS_PER_UNIT;
  186.   register enum machine_mode t, val;
  187.  
  188.   if (size % BITS_PER_UNIT != 0)
  189.     return BLKmode;
  190.  
  191.   if (size > MAX_FIXED_MODE_SIZE)
  192.     return BLKmode;
  193.  
  194.   /* Get the last mode which has this size.  */
  195.   val = BLKmode;
  196.   for (t = QImode; GET_MODE_CLASS (t) == MODE_INT;
  197.        t = (enum machine_mode) ((int) t + 1))
  198.     if (GET_MODE_SIZE (t) == units)
  199.       val = t;
  200.  
  201.   return val;
  202. }
  203.  
  204. /* Return an INTEGER_CST with value V and type from `sizetype'.  */
  205.  
  206. tree
  207. build_int (v)
  208.      int v;
  209. {
  210.   register tree t;
  211.   /* Type-size nodes already made for small sizes.  */
  212.   static tree size_table[33];
  213.  
  214.   if (v < 33 && size_table[v] != 0)
  215.     return size_table[v];
  216.   if (v < 33)
  217.     {
  218.       int temp = allocation_temporary_p ();
  219.       /* Make this a permanent node.  */
  220.       if (temp)
  221.     end_temporary_allocation ();
  222.       t = build_int_2 (v, 0);
  223.       TREE_TYPE (t) = sizetype;
  224.       size_table[v] = t;
  225.       if (temp)
  226.     resume_temporary_allocation ();
  227.     }
  228.   else
  229.     {
  230.       t = build_int_2 (v, 0);
  231.       TREE_TYPE (t) = sizetype;
  232.     }
  233.   return t;
  234. }
  235.  
  236. /* Combine operands OP1 and OP2 with arithmetic operation OPC.
  237.    OPC is a tree code.  Data type is taken from `sizetype',
  238.    If the operands are constant, so is the result.  */
  239.  
  240. tree
  241. genop (opc, op1, op2)
  242.      enum tree_code opc;
  243.      tree op1, op2;
  244. {
  245.   /* Handle the special case of two integer constants faster.  */
  246.   if (TREE_CODE (op1) == INTEGER_CST && TREE_CODE (op2) == INTEGER_CST)
  247.     {
  248.       /* And some specific cases even faster than that.  */
  249.       if (opc == PLUS_EXPR
  250.       && TREE_INT_CST_LOW (op1) == 0
  251.       && TREE_INT_CST_HIGH (op1) == 0)
  252.     return op2;
  253.       if (opc == MINUS_EXPR
  254.       && TREE_INT_CST_LOW (op2) == 0
  255.       && TREE_INT_CST_HIGH (op2) == 0)
  256.     return op1;
  257.       if (opc == MULT_EXPR
  258.       && TREE_INT_CST_LOW (op1) == 1
  259.       && TREE_INT_CST_HIGH (op1) == 0)
  260.     return op2;
  261.       if (opc == CEIL_DIV_EXPR
  262.       && TREE_INT_CST_LOW (op1) == TREE_INT_CST_LOW (op2)
  263.       && TREE_INT_CST_HIGH (op1) == TREE_INT_CST_HIGH (op2))
  264.     return size_one_node;
  265.       /* Handle general case of two integer constants.  */
  266.       return combine (opc, op1, op2);
  267.     }
  268.  
  269.   if (op1 == error_mark_node || op2 == error_mark_node)
  270.     return error_mark_node;
  271.  
  272.   return fold (build (opc, sizetype, op1, op2));
  273. }
  274.  
  275. /* Convert a size which is SIZE when expressed in unit INUNITS
  276.    into the units OUTUNITS.  Rounds up if conversion is not exact.
  277.    If SIZE is constant, so is the result.  */
  278.  
  279. tree
  280. convert_units (size, inunits, outunits)
  281.    tree size;
  282.    register int inunits, outunits;
  283. {
  284.   register tree t;
  285.  
  286.   if (inunits == outunits)
  287.     return size;
  288.   /* Check for inunits divisible by outunits.
  289.      In that case, just multiply by their ratio.  */
  290.   if (0 == (inunits % outunits))
  291.     return genop (MULT_EXPR, size, build_int (inunits / outunits));
  292.   /* The inverse case.  */
  293.   if (0 == (outunits % inunits))
  294.     {
  295.       /* Discard anything in SIZE to round it up to a multiple
  296.      of a number N that divides our current divisor.  */
  297.       if (TREE_CODE (size) == MULT_EXPR
  298.       && TREE_CODE (TREE_OPERAND (size, 1)) == INTEGER_CST
  299.       && 0 == (outunits / inunits) % TREE_INT_CST_LOW (TREE_OPERAND (size, 1))
  300.       && TREE_CODE (TREE_OPERAND (size, 0)) == CEIL_DIV_EXPR
  301.       && tree_int_cst_equal (TREE_OPERAND (size, 1),
  302.                  TREE_OPERAND (TREE_OPERAND (size, 0), 1)))
  303.     size = TREE_OPERAND (TREE_OPERAND (size, 0), 0);
  304.       return genop (CEIL_DIV_EXPR, size, build_int (outunits / inunits));
  305.     }
  306.   /* The general case.  */
  307.   t = genop (MULT_EXPR, size,
  308.          build_int (inunits)); /* convert to bits */
  309.   return genop (CEIL_DIV_EXPR, t,
  310.         build_int (outunits)); /* then to outunits */
  311. }
  312.  
  313. /* Set the size, mode and alignment of a ..._DECL node.
  314.    TYPE_DECL does need this for C++.  It is up to language-specific
  315.    code to intialize the DECL_OFFSET of TYPE_DECL nodes.
  316.    Note that LABEL_DECL and CONST_DECL nodes do not need this,
  317.    and FUNCTION_DECL nodes have them set up in a special (and simple) way.
  318.    Don't call layout_decl for them.
  319.  
  320.    KNOWN_ALIGN is the amount of alignment we can assume this
  321.    decl has with no special effort.  It is relevant only for FIELD_DECLs
  322.    and depends on the previous fields.
  323.    All that matters about KNOWN_ALIGN is which powers of 2 divide it.
  324.    If KNOWN_ALIGN is 0, it means, "as much alignment as you like":
  325.    the record will be aligned to suit.  */
  326.  
  327. void
  328. layout_decl (decl, known_align)
  329.      tree decl;
  330.      unsigned known_align;
  331. {
  332.   register tree type = TREE_TYPE (decl);
  333.   register enum tree_code code = TREE_CODE (decl);
  334.   int spec_size = DECL_SIZE_UNIT (decl);
  335.   int bitsize;
  336.  
  337.   if (code == CONST_DECL)
  338.     return;
  339.  
  340.   if (code != VAR_DECL && code != PARM_DECL && code != RESULT_DECL
  341.       && code != FIELD_DECL && code != TYPE_DECL)
  342.     abort ();
  343.  
  344.   if (type == error_mark_node)
  345.     {
  346.       type = void_type_node;
  347.       spec_size = 0;
  348.     }
  349.   if (TYPE_SIZE_UNIT (type) == 0)
  350.     abort ();
  351.  
  352.   /* Usually the size and mode come from the data type without change.  */
  353.  
  354.   DECL_MODE (decl) = TYPE_MODE (type);
  355.   DECL_SIZE (decl) = TYPE_SIZE (type);
  356.   DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (type);
  357.   TREE_UNSIGNED (decl) = TREE_UNSIGNED (type);
  358.  
  359.   if (code == FIELD_DECL && TREE_PACKED (decl))
  360.     {
  361.       /* This is a bit-field.  We don't know how to handle
  362.      them except for integers and enums, and front end should
  363.      never generate them otherwise.  */
  364.  
  365.       if (! (TREE_CODE (type) == INTEGER_TYPE
  366.          || TREE_CODE (type) == ENUMERAL_TYPE))
  367.     abort ();
  368.  
  369.       if (spec_size == 0)
  370.     abort ();
  371.  
  372.       /* Mode is "integer bit field".  */
  373.       DECL_MODE (decl) = BImode;
  374.       /* Size is specified number of bits.  */
  375.       DECL_SIZE (decl) = size_one_node;
  376.       DECL_SIZE_UNIT (decl) = spec_size;
  377.     }
  378.   /* Force alignment required for the data type.
  379.      But if the decl itself wants greater alignment, don't override that.  */
  380.   else if (TYPE_ALIGN (type) > DECL_ALIGN (decl))
  381.     DECL_ALIGN (decl) = TYPE_ALIGN (type);
  382.  
  383.   if (DECL_SIZE (decl))
  384.     bitsize = TREE_INT_CST_LOW (DECL_SIZE (decl)) * DECL_SIZE_UNIT (decl);
  385.  
  386.   /* See if we can use a scalar mode such as QImode or SImode
  387.      in place of BLKmode or a packed byte mode.  */
  388.   /* Conditions are: a fixed size that is correct for another mode
  389.      and occupying a complete byte or bytes on proper boundary.  */
  390.   if ((DECL_MODE (decl) == BLKmode
  391.        || DECL_MODE (decl) == BImode)
  392.       /* Don't do this if DECL's type requires it to be BLKmode.  */
  393.       && TYPE_MODE (type) != BLKmode
  394.       && TYPE_SIZE (type) != 0
  395.       && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
  396.     {
  397.       register enum machine_mode xmode = agg_mode (bitsize);
  398.  
  399.       if (xmode != BLKmode
  400.       && known_align % GET_MODE_ALIGNMENT (xmode) == 0)
  401.     {
  402.       DECL_ALIGN (decl) = MAX (GET_MODE_ALIGNMENT (xmode),
  403.                    DECL_ALIGN (decl));
  404.       DECL_MODE (decl) = xmode;
  405.       DECL_SIZE (decl) = build_int (GET_MODE_SIZE (xmode));
  406.       DECL_SIZE_UNIT (decl) = BITS_PER_UNIT;
  407.       bitsize = GET_MODE_BITSIZE (xmode);
  408.     }
  409.     }
  410.  
  411.   /* Don't let more than one word of an aggregate occupy one register,
  412.      since then the SUBREG used to access the high part would malfunction.
  413.      Check that the expected # of registers is big enough that they
  414.      seem to hold this variable with just a word per register.  */
  415.   if (DECL_SIZE (decl) != 0
  416.       && (TREE_CODE (type) == RECORD_TYPE
  417.       || TREE_CODE (type) == UNION_TYPE
  418.       || TREE_CODE (type) == ARRAY_TYPE))
  419.     {
  420.       /* This test is not exactly right, since we really want the minimum
  421.      number of regs in any class that can hold this mode.
  422.      But it does distinguish the machines we need to distinguish,
  423.      for now.  */
  424.       if (CLASS_MAX_NREGS (ALL_REGS, TYPE_MODE (type)) * BITS_PER_WORD
  425.       < bitsize)
  426.     TREE_ADDRESSABLE (decl) = 1;
  427.     }
  428.  
  429.   /* Evaluate nonconstant size only once, either now or as soon as safe.  */
  430.   if (DECL_SIZE (decl) != 0 && ! TREE_LITERAL (DECL_SIZE (decl)))
  431.     DECL_SIZE (decl) = variable_size (DECL_SIZE (decl));
  432. }
  433.  
  434. /* Lay out a RECORD_TYPE type (a C struct).
  435.    This means laying out the fields, determining their offsets,
  436.    and computing the overall size and required alignment of the record.
  437.    Note that if you set the TYPE_ALIGN before calling this
  438.    then the struct is aligned to at least that boundary.
  439.  
  440.    If the type has basetypes, you must call layout_basetypes
  441.    before calling this function.  */
  442.  
  443. static void
  444. layout_record (rec)
  445.      tree rec;
  446. {
  447.   register tree field;
  448. #ifdef STRUCTURE_SIZE_BOUNDARY
  449.   int record_align = MAX (STRUCTURE_SIZE_BOUNDARY, TYPE_ALIGN (rec));
  450. #else
  451.   int record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (rec));
  452. #endif
  453.   /* These must be laid out *after* the record is.  */
  454.   tree pending_statics = NULL_TREE;
  455.   /* Record size so far is CONST_SIZE + VAR_SIZE * SIZE_UNIT bits,
  456.      where CONST_SIZE is an integer
  457.      and VAR_SIZE is a tree expression.
  458.      If VAR_SIZE is null, the size is just CONST_SIZE.
  459.      Naturally we try to avoid using VAR_SIZE.  */
  460.   register int const_size = 0;
  461.   register tree var_size = 0;
  462.   register int size_unit = BITS_PER_UNIT;
  463.  
  464. #if 0
  465.   /* If there are basetypes, the caller should already have
  466.      laid them out.  Leave space at the beginning for them.  */
  467.   if (TYPE_SIZE (rec) != 0)
  468.     {
  469.       if (TREE_CODE (TYPE_SIZE (rec)) == INTEGER_CST)
  470.     const_size = TREE_INT_CST_LOW (TYPE_SIZE (rec));
  471.       else
  472.     var_size = TYPE_SIZE (rec);
  473.       size_unit = TYPE_SIZE_UNIT (rec);
  474.     }
  475. #endif /* 0 */
  476.  
  477.   for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
  478.     {
  479.       register int desired_align;
  480.  
  481.       /* If FIELD is a VAR_DECL, then treat it like a separate variable,
  482.      not really like a structure field.
  483.      If it is a FUNCTION_DECL, it's a method.
  484.      In both cases, all we do is lay out the decl,
  485.      and we do it *after* the record is laid out.  */
  486.  
  487.       if (TREE_CODE (field) == VAR_DECL)
  488.     {
  489.       pending_statics = tree_cons (NULL, field, pending_statics);
  490.       continue;
  491.     }
  492.       /* Enumerators and enum types which are local to this class need not
  493.      be laid out.  */
  494.       if (TREE_CODE (field) == CONST_DECL || TREE_CODE (field) == TYPE_DECL)
  495.     continue;
  496.  
  497.       /* Lay out the field so we know what alignment it needs.
  498.      For KNOWN_ALIGN, pass the number of bits from start of record
  499.      or some divisor of it.  */
  500.  
  501.       layout_decl (field, var_size ? size_unit : const_size);
  502.       desired_align = DECL_ALIGN (field);
  503.  
  504.       /* Record must have at least as much alignment as any field.
  505.      Otherwise, the alignment of the field within the record
  506.      is meaningless.  */
  507.  
  508.       record_align = MAX (record_align, desired_align);
  509. #ifdef PCC_BITFIELD_TYPE_MATTERS
  510.       /* In PCC on Vax, Sony, etc., a bit field of declare type `int'
  511.      forces the entire structure to have `int' alignment.  */
  512.       if (DECL_NAME (field) != 0)
  513.     record_align = MAX (record_align, TYPE_ALIGN (TREE_TYPE (field)));
  514. #endif
  515.  
  516.       /* Does this field automatically have alignment it needs
  517.      by virtue of the fields that precede it and the record's
  518.      own alignment?  */
  519.  
  520.       if (const_size % desired_align != 0
  521.       || (size_unit % desired_align != 0
  522.           && var_size))
  523.     {
  524.       /* No, we need to skip space before this field.
  525.          Bump the cumulative size to multiple of field alignment.  */
  526.  
  527.       if (var_size == 0
  528.           || size_unit % desired_align == 0)
  529.         const_size
  530.           = CEIL (const_size, desired_align) * desired_align;
  531.       else
  532.         {
  533.           var_size
  534.         = genop (PLUS_EXPR, var_size,
  535.              build_int (CEIL (const_size, size_unit)));
  536.           const_size = 0;
  537.           var_size = convert_units (var_size, size_unit, desired_align);
  538.           size_unit = desired_align;
  539.         }
  540.     }
  541.  
  542. #ifdef PCC_BITFIELD_TYPE_MATTERS
  543.       if (TREE_CODE (field) == FIELD_DECL
  544.       && TREE_TYPE (field) != error_mark_node)
  545.     {
  546.       int type_align = TYPE_ALIGN (TREE_TYPE (field));
  547.       register tree dsize = DECL_SIZE (field);
  548.       int field_size = TREE_INT_CST_LOW (dsize) * DECL_SIZE_UNIT (field);
  549.  
  550.       /* A bit field may not span the unit of alignment of its type.
  551.          Advance to next boundary if necessary.  */
  552.       if (const_size / type_align
  553.           != (const_size + field_size - 1) / type_align)
  554.         const_size = CEIL (const_size, type_align) * type_align;
  555.     }
  556. #endif
  557.  
  558.       /* Size so far becomes the offset of this field.  */
  559.  
  560.       DECL_OFFSET (field) = const_size;
  561.       DECL_VOFFSET (field) = var_size;
  562.       DECL_VOFFSET_UNIT (field) = size_unit;
  563.  
  564.       /* If this field is an anonymous union,
  565.      give each union-member the same offset as the union has.  */
  566.  
  567.       if (DECL_NAME (field) == 0
  568.       && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
  569.     {
  570.       tree uelt = TYPE_FIELDS (TREE_TYPE (field));
  571.       for (; uelt; uelt = TREE_CHAIN (uelt))
  572.         {
  573.           DECL_FIELD_CONTEXT (uelt) = DECL_FIELD_CONTEXT (field);
  574.           DECL_OFFSET (uelt) = DECL_OFFSET (field);
  575.           DECL_VOFFSET (uelt) = DECL_VOFFSET (field);
  576.           DECL_VOFFSET_UNIT (uelt) = DECL_VOFFSET_UNIT (field);
  577.         }
  578.     }
  579.  
  580.       /* Now add size of this field to the size of the record.  */
  581.  
  582.       {
  583.         register tree dsize = DECL_SIZE (field);
  584.  
  585.     if (TREE_LITERAL (dsize))
  586.       const_size += TREE_INT_CST_LOW (dsize) * DECL_SIZE_UNIT (field);
  587.     else if (var_size == 0)
  588.       {
  589.         var_size = dsize;
  590.         size_unit = DECL_SIZE_UNIT (field);
  591.       }
  592.     else
  593.       {
  594.         register int tunits = MIN (size_unit, DECL_SIZE_UNIT (field));
  595.         var_size
  596.           = genop (PLUS_EXPR,
  597.                convert_units (var_size, size_unit, tunits),
  598.                convert_units (dsize, DECL_SIZE_UNIT (field), tunits));
  599.       }
  600.       }
  601.     }
  602.  
  603.   /* Work out the total size and alignment of the record
  604.      as one expression and store in the record type.
  605.      Round it up to a multiple of the record's alignment.  */
  606.  
  607.   if (var_size == 0)
  608.     TYPE_SIZE (rec)
  609.       = build_int (CEIL (CEIL (const_size, record_align) * record_align,
  610.              size_unit));
  611.   else
  612.     {
  613.       if (const_size)
  614.     var_size
  615.       = genop (PLUS_EXPR, var_size,
  616.            build_int (CEIL (const_size, size_unit)));
  617.       TYPE_SIZE (rec)
  618.     = convert_units (var_size,
  619.              size_unit,
  620.              record_align);
  621.       size_unit = record_align;
  622.     }
  623.  
  624.   TYPE_SIZE (rec) = convert_units (TYPE_SIZE (rec), size_unit,
  625.                    BITS_PER_UNIT);
  626.   TYPE_SIZE_UNIT (rec) = BITS_PER_UNIT;
  627.   TYPE_ALIGN (rec) = MIN (BIGGEST_ALIGNMENT, record_align);
  628.  
  629.   /* Lay out any static members.  This is done now
  630.      because their type may use the record's type.  */
  631.  
  632.   for (field = pending_statics; field; field = TREE_CHAIN (field))
  633.     layout_decl (TREE_VALUE (field), 0);
  634. }
  635.  
  636. /* Lay out a UNION_TYPE type.
  637.    Lay out all the fields, set their offsets to zero,
  638.    and compute the size and alignment of the union (maximum of any field).
  639.    Note that if you set the TYPE_ALIGN before calling this
  640.    then the union align is aligned to at least that boundary.  */
  641.  
  642. static void
  643. layout_union (rec)
  644.      tree rec;
  645. {
  646.   register tree field;
  647. #ifdef STRUCTURE_SIZE_BOUNDARY
  648.   int union_align = STRUCTURE_SIZE_BOUNDARY;
  649. #else
  650.   int union_align = BITS_PER_UNIT;
  651. #endif
  652.  
  653.   /* The size of the union, based on the fields scanned so far,
  654.      is max (CONST_SIZE, VAR_SIZE).
  655.      VAR_SIZE may be null; then CONST_SIZE by itself is the size.  */
  656.   register int const_size = 0;
  657.   register tree var_size = 0;
  658.  
  659.   for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
  660.     {
  661. #if 0 /* This should be in a language-specific file
  662.      since it needs to use language-specific terminology.  */
  663.       if (TREE_STATIC (field))
  664.     {
  665.       error_with_decl (field, "field `%s' declared static in union");
  666.       TREE_STATIC (field) = 0;
  667.     }
  668. #endif
  669.  
  670.       /* Ignore enumerators and enum types local to the union.  */
  671.       if (TREE_CODE (field) == CONST_DECL || TREE_CODE (field) == TYPE_DECL)
  672.     continue;
  673.  
  674.       layout_decl (field, 0);
  675.       DECL_OFFSET (field) = 0;
  676.       DECL_VOFFSET (field) = 0;
  677.       DECL_VOFFSET_UNIT (field) = BITS_PER_UNIT;
  678.  
  679.       /* Union must be at least as aligned as any field requires.  */
  680.  
  681.       union_align = MAX (union_align, DECL_ALIGN (field));
  682.  
  683. #ifdef PCC_BITFIELD_TYPE_MATTERS
  684.       /* On the m88000, a bit field of declare type `int'
  685.      forces the entire union to have `int' alignment.  */
  686.       union_align = MAX (union_align, TYPE_ALIGN (TREE_TYPE (field)));
  687. #endif
  688.  
  689.       /* Set union_size to max (decl_size, union_size).
  690.      There are more and less general ways to do this.
  691.      Use only CONST_SIZE unless forced to use VAR_SIZE.  */
  692.  
  693.       if (TREE_LITERAL (DECL_SIZE (field)))
  694.     const_size = MAX (const_size,
  695.               TREE_INT_CST_LOW (DECL_SIZE (field))
  696.               * DECL_SIZE_UNIT (field));
  697.       else if (var_size == 0)
  698.     var_size = convert_units (DECL_SIZE (field),
  699.                   DECL_SIZE_UNIT (field),
  700.                   BITS_PER_UNIT);
  701.       else
  702.     var_size = genop (MAX_EXPR,
  703.               convert_units (DECL_SIZE (field),
  704.                      DECL_SIZE_UNIT (field),
  705.                      BITS_PER_UNIT),
  706.               var_size);
  707.     }
  708.  
  709.   /* Determine the ultimate size of the union (in bytes).  */
  710.   if (NULL == var_size)
  711.     TYPE_SIZE (rec) = build_int (CEIL (const_size, BITS_PER_UNIT));
  712.   else if (const_size == 0)
  713.     TYPE_SIZE (rec) = var_size;
  714.   else
  715.     TYPE_SIZE (rec) = genop (MAX_EXPR, var_size,
  716.                  build_int (CEIL (const_size, BITS_PER_UNIT)));
  717.  
  718.   /* Determine the desired alignment.  */
  719.   union_align = MIN (BIGGEST_ALIGNMENT, union_align);
  720.   TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), union_align);
  721.  
  722.   /* Round the size up to be a multiple of the required alignment */
  723.   TYPE_SIZE (rec)
  724.     = convert_units (TYPE_SIZE (rec), BITS_PER_UNIT, TYPE_ALIGN (rec));
  725.   TYPE_SIZE_UNIT (rec) = TYPE_ALIGN (rec);
  726. }
  727.  
  728. /* Calculate the mode, size, and alignment for TYPE.
  729.    For an array type, calculate the element separation as well.
  730.    Record TYPE on the chain of permanent or temporary types
  731.    so that dbxout will find out about it.
  732.  
  733.    TYPE_SIZE of a type is nonzero if the type has been laid out already.
  734.    layout_type does nothing on such a type.
  735.  
  736.    If the type is incomplete, its TYPE_SIZE remains zero.  */
  737.  
  738. void
  739. layout_type (type)
  740.      tree type;
  741. {
  742.   int old;
  743.   int temporary = 0;
  744.  
  745.   if (type == 0)
  746.     abort ();
  747.  
  748.   /* Do nothing if type has been laid out before.  */
  749.   if (TYPE_SIZE (type))
  750.     return;
  751.  
  752.   /* Make sure all nodes we allocate are not momentary;
  753.      they must last past the current statement.  */
  754.   old  = suspend_momentary ();
  755.   if (TREE_PERMANENT (type) && allocation_temporary_p ())
  756.     {
  757.       temporary = 1;
  758.       end_temporary_allocation ();
  759.     }
  760.  
  761.   chain_type (type);
  762.  
  763.   switch (TREE_CODE (type))
  764.     {
  765.     case LANG_TYPE:
  766.       /* This kind of type is the responsibility
  767.      of the languge-specific code.  */
  768.       abort ();
  769.  
  770.     case VOID_TYPE:
  771.       TYPE_SIZE (type) = size_zero_node;
  772.       TYPE_SIZE_UNIT (type) = BITS_PER_UNIT;
  773.       TYPE_ALIGN (type) = 1;
  774.       TYPE_MODE (type) = VOIDmode;
  775.       break;
  776.  
  777.     case INTEGER_TYPE:
  778.     case ENUMERAL_TYPE:
  779.       if (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (type)) >= 0)
  780.     TREE_UNSIGNED (type) = 1;
  781.  
  782.       /* What follows is like agg_mode except that it ignores
  783.      MAX_FIXED_MODE_SIZE.  That applies only to structures.  */
  784.       {
  785.     enum machine_mode mode, t;
  786.  
  787.     /* Get the last mode which has this size.  */
  788.     mode = BLKmode;
  789.     for (t = QImode; GET_MODE_CLASS (t) == MODE_INT;
  790.          t = (enum machine_mode) ((int) t + 1))
  791.       if (GET_MODE_BITSIZE (t) == TYPE_PRECISION (type))
  792.         mode = t;
  793.  
  794.     TYPE_MODE (type) = mode;
  795.       }
  796.       TYPE_SIZE (type) = build_int (GET_MODE_SIZE (TYPE_MODE (type)));
  797.       TYPE_SIZE_UNIT (type) = BITS_PER_UNIT;
  798.       TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
  799.       break;
  800.  
  801.     case REAL_TYPE:
  802.       {
  803.     register int prec = TYPE_PRECISION (type);
  804.     if (prec <= GET_MODE_BITSIZE (SFmode))
  805.       TYPE_MODE (type) = SFmode;
  806.     else if (prec <= GET_MODE_BITSIZE (DFmode))
  807.       TYPE_MODE (type) = DFmode;
  808. #ifdef APPLE_HAX
  809.     /* If we can live with 80/96 bits, use extended float. */
  810.     else if (prec <= GET_MODE_BITSIZE (XFmode))
  811.       TYPE_MODE (type) = XFmode;
  812. #endif /* APPLE_HAX */
  813.     else if (prec <= GET_MODE_BITSIZE (TFmode))
  814.       TYPE_MODE (type) = TFmode;
  815.     else
  816.       abort ();
  817.       }
  818.       TYPE_SIZE (type) = build_int (GET_MODE_SIZE (TYPE_MODE (type)));
  819.       TYPE_SIZE_UNIT (type) = BITS_PER_UNIT;
  820.       TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
  821.       break;
  822.  
  823.     case POINTER_TYPE:
  824.     case REFERENCE_TYPE:
  825.       TYPE_MODE (type) = Pmode;
  826.       TYPE_SIZE (type) = build_int (POINTER_SIZE / BITS_PER_UNIT);
  827.       TYPE_SIZE_UNIT (type) = BITS_PER_UNIT;
  828.       TYPE_ALIGN (type) = POINTER_BOUNDARY;
  829.       TREE_UNSIGNED (type) = 1;
  830.       TYPE_PRECISION (type) = POINTER_SIZE;
  831.       break;
  832.  
  833.     case ARRAY_TYPE:
  834.       {
  835.     register tree index = TYPE_DOMAIN (type);
  836.     register tree length;
  837.     register tree element = TREE_TYPE (type);
  838.  
  839. /*     layout_type (element);  */
  840.     build_pointer_type (element);
  841.  
  842.     if (index == 0)
  843.       length = 0;
  844.     else
  845.       length = genop (PLUS_EXPR, size_one_node,
  846.               genop (MINUS_EXPR, TYPE_MAX_VALUE (index),
  847.                  TYPE_MIN_VALUE (index)));
  848.  
  849.     if (TREE_PACKED (type))
  850.       abort ();  /* ??? Not written yet since not needed for C.  */
  851.  
  852.     TYPE_SIZE_UNIT (type) = TYPE_SIZE_UNIT (element);
  853.     if (length && TYPE_SIZE (element))
  854.       TYPE_SIZE (type) = genop (MULT_EXPR, TYPE_SIZE (element), length);
  855.     TYPE_SEP (type) = TYPE_SIZE (element);
  856.     TYPE_SEP_UNIT (type) = TYPE_SIZE_UNIT (element);
  857.     TYPE_ALIGN (type) = MAX (TYPE_ALIGN (element), BITS_PER_UNIT);
  858.     TYPE_MODE (type) = BLKmode;
  859.     if (TYPE_SIZE (type) != 0
  860.         && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
  861.         /* BLKmode elements force BLKmode aggregate;
  862.            else extract/store fields may lose.  */
  863.         && TYPE_MODE (TREE_TYPE (type)) != BLKmode
  864. #ifdef STRICT_ALIGNMENT
  865.         && (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
  866.         || TYPE_ALIGN (type) >= (TREE_INT_CST_LOW (TYPE_SIZE (type))
  867.                      * TYPE_SIZE_UNIT (type)))
  868. #endif
  869.         )
  870.       {
  871.         TYPE_MODE (type)
  872.           = agg_mode (TREE_INT_CST_LOW (TYPE_SIZE (type))
  873.               * TYPE_SIZE_UNIT (type));
  874.       }
  875.     break;
  876.       }
  877.  
  878.     case RECORD_TYPE:
  879.       layout_record (type);
  880.       TYPE_MODE (type) = BLKmode;
  881.       if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
  882.       /* If structure's known alignment is less than
  883.          what the scalar mode would need, and it matters,
  884.          then stick with BLKmode.  */
  885. #ifdef STRICT_ALIGNMENT
  886.       && (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
  887.           || TYPE_ALIGN (type) >= (TREE_INT_CST_LOW (TYPE_SIZE (type))
  888.                        * TYPE_SIZE_UNIT (type)))
  889. #endif
  890.       )
  891.     {
  892.       tree field;
  893.       /* A record which has any BLKmode members must itself be BLKmode;
  894.          it can't go in a register.  */
  895.       for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
  896.         {
  897.           if (TYPE_MODE (TREE_TYPE (field)) == BLKmode)
  898.         goto record_lose;
  899.  
  900.           /* Must be BLKmode if any field crosses a word boundary,
  901.          since extract_bit_field can't handle that in registers.  */
  902.           if (DECL_OFFSET (field) / BITS_PER_WORD
  903.           != ((TREE_INT_CST_LOW (DECL_SIZE (field)) * DECL_SIZE_UNIT (field)
  904.                + DECL_OFFSET (field) - 1)
  905.               / BITS_PER_WORD))
  906.         goto record_lose;
  907.         }
  908.       
  909.       TYPE_MODE (type)
  910.         = agg_mode (TREE_INT_CST_LOW (TYPE_SIZE (type))
  911.             * TYPE_SIZE_UNIT (type));
  912.     record_lose: ;
  913.     }
  914.       break;
  915.  
  916.     case UNION_TYPE:
  917.       layout_union (type);
  918.       TYPE_MODE (type) = BLKmode;
  919.       if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
  920.       /* If structure's known alignment is less than
  921.          what the scalar mode would need, and it matters,
  922.          then stick with BLKmode.  */
  923. #ifdef STRICT_ALIGNMENT
  924.       && (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
  925.           || TYPE_ALIGN (type) >= (TREE_INT_CST_LOW (TYPE_SIZE (type))
  926.                        * TYPE_SIZE_UNIT (type)))
  927. #endif
  928.       )
  929.     {
  930.       tree field;
  931.       /* A union which has any BLKmode members must itself be BLKmode;
  932.          it can't go in a register.  */
  933.       for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
  934.         if (TYPE_MODE (TREE_TYPE (field)) == BLKmode)
  935.           goto union_lose;
  936.  
  937.       TYPE_MODE (type)
  938.         = agg_mode (TREE_INT_CST_LOW (TYPE_SIZE (type))
  939.             * TYPE_SIZE_UNIT (type));
  940.     union_lose: ;
  941.     }
  942.       break;
  943.  
  944.     case FUNCTION_TYPE:
  945.     case METHOD_TYPE:
  946.       TYPE_MODE (type) = EPmode;
  947.       TYPE_SIZE (type) = build_int (2 * POINTER_SIZE / BITS_PER_UNIT);
  948.       TYPE_SIZE_UNIT (type) = BITS_PER_UNIT;
  949.       TYPE_ALIGN (type) = POINTER_BOUNDARY;
  950.       break;
  951.  
  952.     default:
  953.       abort ();
  954.     } /* end switch */
  955.  
  956.   /* Evaluate nonconstant size only once, either now or as soon as safe.  */
  957.   if (TYPE_SIZE (type) != 0 && ! TREE_LITERAL (TYPE_SIZE (type)))
  958.     TYPE_SIZE (type) = variable_size (TYPE_SIZE (type));
  959.  
  960.   /* Also layout any other variants of the type.  */
  961.   if (TYPE_NEXT_VARIANT (type)
  962.       || type != TYPE_MAIN_VARIANT (type))
  963.     {
  964.       tree variant;
  965.       /* Record layout info of this variant.  */
  966.       tree size = TYPE_SIZE (type);
  967.       int size_unit = TYPE_SIZE_UNIT (type);
  968.       int align = TYPE_ALIGN (type);
  969.       enum machine_mode mode = TYPE_MODE (type);
  970.  
  971.       /* Copy it into all variants.  */
  972.       for (variant = TYPE_MAIN_VARIANT (type);
  973.        variant;
  974.        variant = TYPE_NEXT_VARIANT (variant))
  975.     {
  976.       TYPE_SIZE (variant) = size;
  977.       TYPE_SIZE_UNIT (variant) = size_unit;
  978.       TYPE_ALIGN (variant) = align;
  979.       TYPE_MODE (variant) = mode;
  980.     }
  981.     }
  982.     
  983.   if (temporary)
  984.     resume_temporary_allocation ();
  985.   resume_momentary (old);
  986. }
  987.  
  988. /* Create and return a type for signed integers of PRECISION bits.  */
  989.  
  990. tree
  991. make_signed_type (precision)
  992.      int precision;
  993. {
  994.   register tree type = make_node (INTEGER_TYPE);
  995.  
  996.   TYPE_PRECISION (type) = precision;
  997.  
  998.   /* Create the extreme values based on the number of bits.  */
  999.  
  1000.   TYPE_MIN_VALUE (type)
  1001.     = build_int_2 ((precision-HOST_BITS_PER_INT > 0 ? 0 : (-1)<<(precision-1)),
  1002.            (-1)<<(precision-HOST_BITS_PER_INT-1 > 0
  1003.               ? precision-HOST_BITS_PER_INT-1
  1004.               : 0));
  1005.   TYPE_MAX_VALUE (type)
  1006.     = build_int_2 ((precision-HOST_BITS_PER_INT > 0 ? -1 : (1<<(precision-1))-1),
  1007.            (precision-HOST_BITS_PER_INT-1 > 0
  1008.             ? (1<<(precision-HOST_BITS_PER_INT-1))-1
  1009.             : 0));
  1010.  
  1011.   /* Give this type's extreme values this type as their type.  */
  1012.  
  1013.   TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
  1014.   TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
  1015.  
  1016.   /* The first type made with this or `make_unsigned_type'
  1017.      is the type for size values.  */
  1018.  
  1019.   if (sizetype == 0)
  1020.     sizetype = type;
  1021.  
  1022.   /* Lay out the type: set its alignment, size, etc.  */
  1023.  
  1024.   layout_type (type);
  1025.  
  1026.   return type;
  1027. }
  1028.  
  1029. /* Create and return a type for unsigned integers of PRECISION bits.  */
  1030.  
  1031. tree
  1032. make_unsigned_type (precision)
  1033.      int precision;
  1034. {
  1035.   register tree type = make_node (INTEGER_TYPE);
  1036.  
  1037.   TYPE_PRECISION (type) = precision;
  1038.  
  1039.   /* The first type made with this or `make_unsigned_type'
  1040.      is the type for size values.  */
  1041.  
  1042.   if (sizetype == 0)
  1043.     sizetype = type;
  1044.  
  1045.   fixup_unsigned_type (type);
  1046.   return type;
  1047. }
  1048.  
  1049. /* Set the extreme values of TYPE based on its precision in bits,
  1050.    the lay it out.  This is used both in `make_unsigned_type'
  1051.    and for enumeral types.  */
  1052.  
  1053. void
  1054. fixup_unsigned_type (type)
  1055.      tree type;
  1056. {
  1057.   register int precision = TYPE_PRECISION (type);
  1058.  
  1059.   TYPE_MIN_VALUE (type) = build_int_2 (0, 0);
  1060.   TYPE_MAX_VALUE (type)
  1061.     = build_int_2 (precision-HOST_BITS_PER_INT >= 0 ? -1 : (1<<precision)-1,
  1062.            precision-HOST_BITS_PER_INT > 0
  1063.            ? ((unsigned) ~0
  1064.               >> (HOST_BITS_PER_INT - (precision - HOST_BITS_PER_INT)))
  1065.            : 0);
  1066.   TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
  1067.   TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
  1068.  
  1069.   /* Lay out the type: set its alignment, size, etc.  */
  1070.  
  1071.   layout_type (type);
  1072. }
  1073.